<?php
/**
 * Class to handle all unhandled errors in script processing. This is the
 * goalie for all uncaught exceptions and PHP errors, routing them to log files
 * or, more likely, dieing.
 *
 * @package Framework
 * @subpackage Control
 * @author Ryan Somma 08/21/2008
 */
class ErrorHandler
{

    /**
     * Sets the PHP error handler.
     */
    public function setErrorHandler()
    {
        if (ApplicationSession::getValue('debugFlag'))
        {
            error_reporting(E_ALL);
            set_error_handler(array($this, 'catchErrors'), E_ALL);
        }
        else
        {
            error_reporting(E_ERROR);
            set_error_handler(array($this, 'catchErrors'), E_ERROR);
        }
    }

    /**
     * Attempts to handle all errors based on type.
     *
     * @param string $errno
     * @param string $errstr
     * @param string $errfile
     * @param string $errline
     * @param string $errcontext
     * @return string
     */
    public function catchErrors(
        $errno,
        $errstr     = '',
        $errfile    = '',
        $errline    = '',
        $errcontext = ''
    )
    {

        switch ($errno)
        {
            /**
             * Fatal run-time errors.
             * These indicate errors that can not be recovered from, such as a memory allocation problem.
             * Execution of the script is halted.
             */
            case E_ERROR:
                $errorType = 'E_ERROR';
                break;
            /**
             * Run-time warnings (non-fatal errors).
             * Execution of the script is not halted.
             */
            case E_WARNING:
                $errorType = 'E_WARNING';
                break;
            /**
             * Compile-time parse errors.
             * Parse errors should only be generated by the parser.
             */
            case E_PARSE:
                $errorType = 'E_PARSE';
                break;
            /**
             * Run-time notices.
             * Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
             */
            case E_NOTICE:
                $errorType = 'E_NOTICE';
                break;
            /**
             * Fatal errors that occur during PHP's initial startup.
             * This is like an E_ERROR, except it is generated by the core of PHP.
             */
            case E_CORE_ERROR:
                $errorType = 'E_CORE_ERROR';
                break;
            /**
             * Warnings (non-fatal errors) that occur during PHP's initial startup.
             * This is like an E_WARNING, except it is generated by the core of PHP.
             */
            case E_CORE_WARNING:
                $errorType = 'E_CORE_WARNING';
                break;
            /**
             * Fatal compile-time errors.
             * This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
             */
            case E_COMPILE_ERROR:
                $errorType = 'E_COMPILE_ERROR';
                break;
            /**
             * Compile-time warnings (non-fatal errors).
             * This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
             */
            case E_COMPILE_WARNING:
                $errorType = 'E_COMPILE_WARNING';
                break;
            /**
             * User-generated error message.
             * This is like an E_ERROR, except it is generated in PHP code by
             * using the PHP function trigger_error().
             */
            case E_USER_ERROR:
                $errorType = 'E_USER_ERROR';
                break;
            /**
             * User-generated warning message.
             * This is like an E_WARNING, except it is generated in PHP code by
             * using the PHP function trigger_error().
             */
            case E_USER_WARNING:
                $errorType = 'E_USER_WARNING';
                break;
            /**
             * User-generated notice message.
             * This is like an E_NOTICE, except it is generated in PHP code by
             * using the PHP function trigger_error().
             */
            case E_USER_NOTICE:
                $errorType = 'E_USER_NOTICE';
                break;
            /**
             * Enable to have PHP suggest changes to your code which will ensure the
             * best interoperability and forward compatibility of your code.
             */
            case E_STRICT:
                $errorType = 'E_STRICT';
                break;
            /**
             * Catchable fatal error. It indicates that a probably dangerous
             * error occured, but did not leave the Engine in an unstable state.
             * If the error is not caught by a user defined handle (see also
             * set_error_handler()), the application aborts as it was an E_ERROR.
             */
            case E_RECOVERABLE_ERROR:
                $errorType = 'E_RECOVERABLE_ERROR';
                break;
            /**
             * Run-time notices. Enable this to receive warnings about code that
             * will not work in future versions.
             */
            case E_DEPRECATED:
                $errorType = 'E_DEPRECATED';
                break;
            /**
             * User-generated warning message. This is like an E_DEPRECATED, except it
             * is generated in PHP code by using the PHP function trigger_error().
             */
            case E_USER_DEPRECATED:
                $errorType = 'E_USER_DEPRECATED';
                break;
            default:
                $errorType = 'UNKNOWN';
                break;
        }

        try
        {
            throw new SystemExceptionPHPError
                      (
                          $errorType . " " . $errstr
                          ,0
                          ,$errno
                          ,$errfile
                          ,$errline
                          ,$errcontext
                      );
        }
        catch (SystemExceptionPHPError $e)
        {
            //EXCEPTION LOGS AND HANDLES ITSELF
        }
    }
}
